<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: TUserInfos.php
// +----------------------------------------------------------------------
// | Author: gxs
// +----------------------------------------------------------------------
// | Description: 用于详细说明此程序文件完成的主要功能，与其他模块或函数的接口、输出值、取值范围、含义及参数间的控制、顺序或依赖等关系。
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019/2/27 Author: gxs Modification: 创建
// +----------------------------------------------------------------------

namespace App\ModelTransfers\WsUsers;

use App\Models\User;
use App\Models\UserGlobal;
use App\ModelTransfers\Base;
use App\ModelTransfers\Traits\TUser;
use App\ModelTransfers\TransferInterface\TransferInterface;
use App\ModelTransfers\WsAccount\AccountUserCard;
use App\ModelTransfers\WsAccount\PayBindCard;
use App\ModelTransfers\WsCommon\CommonAttachment;
use App\ModelTransfers\WsLoans\LoanApply;
use Illuminate\Support\Facades\DB;

class TUserInfos extends Base
{
    use TUser;

    protected $connection = 'transfer';

    protected $table = 'ws_user.t_user_info'; //数据库.表名

    public $incrementing = false;


    /**
     * Description: 移除迁移用户
     * Author: liuju
     * DateTime: 2019-03-05 17:29
     * @param $userId
     * @return int
     */

    public static function removeTransfer($userId = '')
    {
        return DB::table('users')->whereNotNull('old_kskd_user_id')
            ->when($userId,function($query) use($userId){
            return $query->where('id',$userId);
        })->delete();
    }

    /**
     * Description: 获取oss关联头像
     * Author: liuju
     * DateTime: 2019-02-27 18:13
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function avatar()
    {
        return $this->hasOne(CommonAttachment::class, 'user_id', 'id')->select('oss_url')->where(['ws_common.common_attachment.biz_type' => 'head-pic'])->orderByDesc('id');
    }

    /**
     * Description: 获取用户绑定的银行卡
     * Author: liuju
     * DateTime: 2019-02-28 09:46
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function bindCard()
    {
        return $this->hasOne(AccountUserCard::class, 'user_id', 'id')->select('bank_card_no')->where('is_acquiesce', 1)->orderByDesc('id');
    }

    public function apply()
    {
        return $this->hasOne(LoanApply::class, 'user_id', 'id')->orderByDesc('create_time')->select('apply_status');
    }

    public static function removeUserGlobal($userId)
    {
        return  UserGlobal::whereNotNull('old_kskd_user_id')
            ->when($userId,function($query) use($userId){
                return $query->where('uid',$userId);
            })
            ->delete();
    }


}